<?php
/**
 * MYSQL数据库操作函数库
 * @author z.En Wong
 * @version 1.0
 * @data 07/14/2009
 *
 */


class JL_mysql_table extends stdClass {
	
	private $table_name = '';
	
	private $charset = '';
	
	private $sql = null;
	
	private $db_link = null;
	private $is_pconnect = null;
	
	private function _pconnect(){
		$GLOBALS['jl_db_link'] = mysql_pconnect(DB_HOST,DB_USERNAME,DB_PASSWORD);
		mysql_select_db(DB_DBNAME,$GLOBALS['jl_db_link']);
		$this->db_link = &$GLOBALS['jl_db_link'];
		$GLOBALS['jl_db_is_pconnect'] = false;
		$this ->is_pconnect = &$GLOBALS['jl_db_is_pconnect'];
	}
	
	private function _connect(){
		$GLOBALS['jl_db_link'] = mysql_connect(DB_HOST,DB_USERNAME,DB_PASSWORD);
		mysql_select_db(DB_DBNAME,$GLOBALS['jl_db_link']);
		$this->db_link = &$GLOBALS['jl_db_link'];
		$GLOBALS['jl_db_is_pconnect'] = false;
		$this -> is_pconnect = &$GLOBALS['jl_db_is_pconnect'];
	}
	
	private function _close(){
		if ($this->is_pconnect === true) {
			mysql_close($this->db_link);
		}
		
	}
	
	
	public function __construct($table_name = ''){
		$this->JL_mysql_table($table_name);
		
	}
	
	public function JL_mysql_table($table_name = ''){
		$this->table_name = $table_name;
	}
	
	public function __destruct(){
		$this->_close();
	}
	
	
	public function charset($charset){
		if (empty($charset)) {
			$charset = DB_CHAR_SET;
		}
		$this->charset = $charset;
		
	}
	
	public function get_rows($fidle_str,$condition=''){
		
		
		$sql = "SELECT {$fidle_str} FROM `".$this->table_name."` {$condition}";
		$this->sql = $sql;
		
//		$this->where = $where;
		
	}
	
		
//	public function set_fidle($fidle_str){
//		
//		$this->sql = str_replace('*',' '.$fidle_str.' ',$this->sql);
//		
//	}
	
	public function fetch($res){
		
		$a = array();
		while ($row = mysql_fetch_assoc($res)) {
			$a[] = $row;
		}
		return $a;
		
	}
	
	public function delete_rows($where){
		
		$sql = "DELETE FROM `".$this->table_name."` WHERE {$where}";
		$this->sql = $sql;
		
	}
	
	public function new_rows($data_arr){
		
		//如果是二维数组
		if (isset($data_arr[0]) && is_array($data_arr[0])) {
			foreach ($data_arr as $item_data){
				
				$db_key = '';
				$db_value = '';
				foreach ($item_data as $k => $v){
					$db_key .= "`{$k}`,";
					$db_value .= "'".mysql_escape_string($v)."',";
				}
				$db_key = trim($db_key,',');
				$db_value = trim($db_value,',');
				
				$sql = "INSERT INTO `".$this->table_name."` ( {$db_key} ) VALUES ( {$db_value} ) ";
				$this->sql[] = $sql;
	
			}
			
			
		}else{
			$db_key = '';
			$db_value = '';
			foreach ($data_arr as $k => $v){
				$db_key .= "`{$k}`,";
				$db_value .= "'".mysql_escape_string($v)."',";

			}
			$db_key = trim($db_key,',');
			$db_value = trim($db_value,',');
			
			$sql = "INSERT INTO `".$this->table_name."` ( {$db_key} ) VALUES ( {$db_value} ) ";
			
			$this->sql = $sql;
		}
		
		
		
	}
	
	public function set_rows($data_arr,$where,$do_escape=true){
		
		$sql = "UPDATE `".$this->table_name."` SET " ;
		
		if($do_escape){
			foreach ($data_arr as $k => $v){
			
				$sql .= "`{$k}` = '" .mysql_escape_string($v). "',";
			
			}
		}else{
			foreach ($data_arr as $k => $v){
			
				$sql .= "`{$k}` = '{$v}',";
			
			}
		}

		$sql = trim($sql,',');
		
		$sql .= " WHERE {$where}";
		
		$this->sql = $sql;
		
		
	}

	public function query($sql){
		$this->sql = $sql;

	}	
	
	public function exec(){

		if (empty($GLOBALS['jl_db_link'])) {
			
			if (DB_IS_PCONNECT===true) {
				$this->_pconnect();
			}else{
				$this->_connect();
			}
		}else{
			$this->db_link = &$GLOBALS['jl_db_link'];
			$this->is_pconnect = &$GLOBALS['jl_db_is_pconnect'];
		}
		
		//charset
		$charset = DB_CHAR_SET;
		mysql_query("SET character_set_connection={$charset}, character_set_results={$charset}, character_set_client=binary",$this->db_link);
		
		if (is_array($this->sql)) {
			$return = array();
			foreach ($this->sql as $sql){
				
				$return_arr[] = mysql_query($sql,$this->db_link);
				$GLOBALS['jl_db_q_count']++;
			}
			
		}else{
			
			$return =  mysql_query($this->sql,$this->db_link);
			$GLOBALS['JL_db_q_count']++;
		}
		
		return $return;
		
		
		
		
		
	}
	
	
	
	public function get_sql(){
		return $this->sql;
	}
	
	
}





?>
